"use strict";
const path = require("path");
const webpack = require("webpack");

function resolve(dir) {
  return path.join(__dirname, dir);
}

console.log(
  `\n\t\x1B[1m当前运行环境：\x1B[22m \x1B[32m${process.env.VUE_APP_ENV}\x1B[39m\n`
);
console.log(
  `\n\t\x1B[1m当前接口前缀：\x1B[22m \x1B[32m${process.env.VUE_APP_SERVER}\x1B[39m\n`
);

module.exports = {
  publicPath: "./",
  lintOnSave: false,
  devServer: {
    disableHostCheck: true,
    port: 8080,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    }
    // proxy: {
    //   "/API": {
    //     target: process.env.VUE_APP_SERVER,
    //     changeOrigin: true,
    //     ws: true,
    //     pathRewrite: {
    //       "^/API": ""
    //     }
    //   }
    // }
  },
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: {
            "primary-color": "#1890ff",
            "link-color": "#1890ff",
            "warning-color": "#faad14",
            "success-color": "#52c41a",
            "error-color": "#f5222d",
            "heading-color": "rgba(0, 0, 0, 0.85)",
            "text-color": "rgba(0, 0, 0, 0.65)",
            "disabled-color": "rgba(0, 0, 0, 0.25)",
            "menu-dark-title": "#333f57",
            "menu-dark-active-bg": "#242e42",
            "layout-sider-background": "#333f57",
            "menu-select-backgrounde": "#0091ff"
          },
          javascriptEnabled: true
        }
      }
    }
  },
  configureWebpack: (config) => {
    if (process.env.NODE_ENV === "production") {
      // 为生产环境修改配置...
      config.mode = "production";
      config["performance"] = {
        //打包文件大小配置
        maxEntrypointSize: 10000000,
        maxAssetSize: 30000000
      };
    }
  },
  chainWebpack(config) {
    config.resolve.symlinks(true);
    config.resolve.alias.set("@", resolve("src"));
    config
      .plugin("ignore")
      // 忽略/moment/locale下的所有文件
      .use(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
    config.plugin("preload").tap(() => [
      {
        rel: "preload",
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: "initial"
      }
    ]);
    config.plugins.delete("prefetch");
    config.module.rule("svg").exclude.add(resolve("src/icons")).end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]"
      })
      .end();

    config.when(process.env.NODE_ENV !== "development", (config) => {
      config
        .plugin("ScriptExtHtmlWebpackPlugin")
        .after("html")
        .use("script-ext-html-webpack-plugin", [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            inline: /runtime\..*\.js$/
          }
        ])
        .end();
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial"
          },
          elementUI: {
            name: "chunk-ant",
            priority: 20,
            test: /[\\/]node_modules[\\/]_?antd(.*)/
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          }
        }
      });
      config.optimization.runtimeChunk("single");
    });
  },
  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: ["./src/styles/mixin.less"]
    }
  }
};
